//
//  YYStarView.m
//  BadBoy
//
//  Created by wen on 2017/3/25.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "YYStarView.h"

@interface YYStarView ()
{
    float starRating;
    float lastRating;
    
    float height;
    float width;
    float space;
    
    UIImage *unSelectedImage;
    UIImage *halfSelectedImage;
    UIImage *fullSelectedImage;
}

@property (nonatomic,weak) id<YYStarViewDelegate> delegate;

@end

@implementation YYStarView

static NSString *deselectedName = @"xin";
static NSString *halfSelectedName = @"xin_sel";
static NSString *fullSelectedName = @"xin_sel";

- (instancetype)init{
    if (self = [super init]) {
        
        unSelectedImage = [UIImage imageNamed:deselectedName];
        halfSelectedImage = halfSelectedName == nil ? unSelectedImage : [UIImage imageNamed:halfSelectedName];
        fullSelectedImage = [UIImage imageNamed:fullSelectedName];
        
        for (int i = 0;  i< 5; i++) {
            UIImageView *imagView = [[UIImageView alloc] init];
            imagView.image = unSelectedImage;
            imagView.userInteractionEnabled = NO;
            imagView.tag = 2222 + i;
            [self addSubview:imagView];
        }
    }
    return self;
}

/**
 *  初始化设置未选中图片、半选中图片、全选中图片，以及评分值改变的代理（可以用
 *  Block）实现
 *
 *  @param delegate          代理
 */
-(void)setStarWidth:(CGFloat)starWidth starSpace:(CGFloat)starSpace andDelegate:(id<YYStarViewDelegate>)delegate{
    
    self.delegate = delegate;
    
    height = starWidth,width = starWidth,space = starSpace;
    
    for (int i = 0;  i< 5; i++) {
        UIImageView *imagView = [self viewWithTag:2222 + i];
        imagView.frame = CGRectMake(i * (width+space), 0, width, height);
    }
    
    CGRect frame = [self frame];
    frame.size.width = (width+space) * 5;
    frame.size.height = height;
    [self setFrame:frame];
    
    self.isIndicator = YES;
}

/**
 *  设置评分值
 *
 *  @param rating 评分值
 */
-(void)displayRating:(float)rating{
    
    for (int i = 0; i < 5; i++) {
        UIImageView *imagView = [self viewWithTag:2222 + i];
        if (rating >= i+1) {
            [imagView setImage:fullSelectedImage];
        }else if (rating >= i + 0.5) {
            [imagView setImage:halfSelectedImage];
        }else{
            [imagView setImage:unSelectedImage];
        }
    }
    starRating = rating;
    lastRating = rating;
    [_delegate ratingChanged:rating];
}

- (void)setIsIndicator:(BOOL)isIndicator{
    _isIndicator = isIndicator;
    self.userInteractionEnabled = !isIndicator;
}

/**
 *  获取当前的评分值
 *
 *  @return 评分值
 */
-(float)rating{
    return starRating;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.isIndicator) {
        return;
    }
    CGPoint point = [[touches anyObject] locationInView:self];
    int newRating = (int) (point.x / (width + space)) + 1;
    if (newRating > 5)
        return;
    
    if (point.x < 0) {
        newRating = 0;
    }
    if (newRating != lastRating){
        [self displayRating:newRating];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if (self.isIndicator) {
        return;
    }
    
    CGPoint point = [[touches anyObject] locationInView:self];
    int newRating = (int) (point.x / (width + space)) + 1;
    if (newRating > 5)
        return;
    
    if (point.x < 0) {
        newRating = 0;
    }
    if (newRating != lastRating){
        [self displayRating:newRating];
    }
}

@end
